package clue.editor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class PlayersPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JButton addPlayerButton;
	private JPanel playersInnerPanel;
	private JTextField errorTextField;
	
	public PlayersPanel() 
	{
		setBorder(BorderFactory.createTitledBorder("Players:"));
		setLayout(new BorderLayout(0, 0));
		addPlayerButton = new JButton("Add Player");
		add(addPlayerButton, BorderLayout.NORTH);
		addPlayerButton.addActionListener(getAddPlayerAction());
			
		playersInnerPanel = new JPanel();
		playersInnerPanel.setLayout(new GridLayout(0, 3, 10, 0));

		JScrollPane scroller = new JScrollPane(playersInnerPanel);
		scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add(scroller);
		
		errorTextField = new JTextField();
		add(errorTextField, BorderLayout.SOUTH);
		errorTextField.setColumns(10);
		errorTextField.addActionListener(getChangeTextAction());
		
	}

	/**
	 * The method adds a player panel to the board
	 */
	public void addPlayer() {
		PlayerPanel playerPanel = new PlayerPanel(this);
		playersInnerPanel.add(playerPanel);
		
		GlobalHistory.getInstance().setDisabled(true);
		playerPanel.setOrder(playersInnerPanel.getComponentCount());
		GlobalHistory.getInstance().setDisabled(false);
		validate();
		repaint();
	}

	/**
	 * The method removes a player panel from the board
	 */
	public void removePlayer(ActionEvent e) {
		JButton removeButton = ((JButton) e.getSource());
		PlayerPanel playerPanel2 =  (PlayerPanel)removeButton.getParent();
		GlobalHistory.getInstance().setDisabled(true);
		
		for (int i = playerPanel2.getOrder(); i < playersInnerPanel.getComponentCount(); i++) {
			
			((PlayerPanel)playersInnerPanel.getComponent(i)).setOrder(i);
			
		}
		
		GlobalHistory.getInstance().setDisabled(false);
		playerPanel2.getParent().remove(playerPanel2);
		
		
		
		validate(); 
		repaint();
	}

	/**
	 * The method removes all the players 
	 */
	public void removeAllPlayers()
	{
		for (Component component : playersInnerPanel.getComponents()) {
			playersInnerPanel.remove(component);
		}
		
		validate();
		repaint();
	}

	/**
	 * The method invoked when the user presses add player
	 * @return
	 */
	public ActionListener getAddPlayerAction() {
		return new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				// Check if there are already 6 players on the board
				if (playersInnerPanel.getComponentCount() == 6)
				{
					ClueFrame.getInstance().setMessage("Can not add more then 6 players", Color.red);
				}
				else
				{
					addPlayer();
					ClueFrame.getInstance().addEdit();
				}
			}
		};
	}
	
	/**
	 * The method invoked when the user changes the player's name
	 * @return
	 */
	public ActionListener getChangeTextAction() {
		return new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ClueFrame.getInstance().addEdit();
			}
		};
	}

	public JPanel getPlayersInnerPanel() {
		return playersInnerPanel;
	}

	public void setPlayersInnerPanel(JPanel playersInnerPanel) {
		this.playersInnerPanel = playersInnerPanel;
	}

	public void changeErrorText(String errorText, Color color)
	{
		errorTextField.setText(errorText);
		errorTextField.setForeground(color);
		validate();
		repaint();
	}
}
